package com.medical.service.impl;

import com.medical.mapper.SubitemUsageMapper;
import com.medical.pojo.SubitemUsage;
import com.medical.service.SubitemUsageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SubitemUsageServiceImpl implements SubitemUsageService {

    @Autowired
    private SubitemUsageMapper subitemUsageMapper;
    @Override
    public List<SubitemUsage> findAll(SubitemUsage subitemUsage) {
        return subitemUsageMapper.findAll(subitemUsage);
    }

    @Override
    public List<List<String>> findAllItem(SubitemUsage subitemUsage) {
        List<SubitemUsage> list = subitemUsageMapper.findAll(subitemUsage);

        List<String> lightingUsage = list.stream().map(SubitemUsage::getLightingUsage).collect(Collectors.toList());
        List<String> conditioningUsage = list.stream().map(SubitemUsage::getConditioningUsage).collect(Collectors.toList());
        List<String> powerUsage = list.stream().map(SubitemUsage::getPowerUsage).collect(Collectors.toList());
        List<String> specialUsage = list.stream().map(SubitemUsage::getSpecialUsage).collect(Collectors.toList());


        List<List<String>> allItem = new ArrayList<>();

        allItem.add(lightingUsage);
        allItem.add(conditioningUsage);
        allItem.add(powerUsage);
        allItem.add(specialUsage);
        return allItem;
    }


    @Override
    public List<Map<String,String>> findPieChartItem(SubitemUsage subitemUsage) {
        ArrayList<Map<String,String>> arrayList = new ArrayList<>();
        List<SubitemUsage> list = subitemUsageMapper.findAll(subitemUsage);

        List<String> lightingUsage = list.stream().map(SubitemUsage::getLightingUsage).collect(Collectors.toList());
        List<String> conditioningUsage = list.stream().map(SubitemUsage::getConditioningUsage).collect(Collectors.toList());
        List<String> powerUsage = list.stream().map(SubitemUsage::getPowerUsage).collect(Collectors.toList());
        List<String> specialUsage = list.stream().map(SubitemUsage::getSpecialUsage).collect(Collectors.toList());

        int lightingSum = 0;
        for (String lighting : lightingUsage) {
            lightingSum += Integer.parseInt(lighting);
        }
        int conditioningSum = 0;
        for (String conditioning : conditioningUsage) {
            conditioningSum += Integer.parseInt(conditioning);
        }
        int powerSum = 0;
        for (String power : powerUsage) {
            powerSum += Integer.parseInt(power);
        }
        int specialSum = 0;
        for (String special : specialUsage) {
            specialSum += Integer.parseInt(special);
        }
        List<String> names = new ArrayList<>();
        names.add("照明耗能");
        names.add("空调耗能");
        names.add("动力耗能");
        names.add("特殊耗能");
        List<Integer> sums = new ArrayList<>();
        sums.add(lightingSum);
        sums.add(conditioningSum);
        sums.add(powerSum);
        sums.add(specialSum);
        for (int i = 0; i < sums.size(); i++) {
            HashMap<String, String> map = new HashMap<>();
            map.put("value", sums.get(i)+"");
            map.put("name",names.get(i));
            arrayList.add(map);
        }

        return arrayList;
    }
}
